
import rcontrolutil
import rcontrol
import Tkinter


class RFrame(rcontrol.RControl):
	"""
	Frame control. Does not really do anything beyond what the Tkinter frame does.
	
	Properties:
	
	*bgcolor* - Background color of the label. RGB triplet, such as (255,0,0) for pure red.
	*borderwidth* - An integer, width of border in pixels.
	*borderstyle* - Border style. One of 'sunken','raised','groove','ridge','flat'. Default is 'sunken'.
	*width* - Set width. If set to 0, width is based on contents (the default behavior).
	*height* - Set height. If set to 0, height is based on contents (the default behavior).
	"""
	
	_basecontrol = None
	
	_pack_children_orientation = rcontrolutil.H #used by RWindow class to pack children.
	_exitWithCallback = None #used to signal back to the window that we left a "with" block.
	_children_layoutmethod = None #used to help check if you're trying to pack() and grid() at the same time
	
	def __init__(self,orient=rcontrolutil.H,label_frame_text=None, _exitWithCallback=None,parent=None, **options):
		if label_frame_text:
			self._newWidgetManual(parent, Tkinter.LabelFrame)
			self._basecontrol.configure(text=label_frame_text)
		else:
			self._newWidgetManual(parent, Tkinter.Frame)
		
		self._pack_children_orientation = orient
		self._exitWithCallback = _exitWithCallback
		self._children_layoutmethod = None
		
		self.set_many(**options)
	
	_properties = { 
		# Property name,  Tkinter property name, SET wrapper, GET wrapper
		'bgcolor': ('background',rcontrolutil.mapColor),
		'width': 'width',
		'height': 'height',
		'borderwidth': 'borderwidth',
		'borderstyle': ('relief', rcontrolutil.mapBorderstyle),
		}
		
	def set_col_weight(self, ncol, weight=None, minsize=None):
		opts = {}
		if weight: opts['weight'] = weight
		if minsize: opts['minsize'] = minsize
		self._basecontrol.grid_columnconfigure(ncol, **opts)
	def set_row_weight(self, nrow, weight=None, minsize=None):
		opts = {}
		if weight: opts['weight'] = weight
		if minsize: opts['minsize'] = minsize
		self._basecontrol.grid_rowconfigure(nrow, **opts)
	
	# These special methods allow the "with w.add_frame()" syntax.
	def __enter__(self):
		if self._exitWithCallback==0: raise rcontrolutil.RException('You cannot reopen a closed RFrame.')
		if self._exitWithCallback==None: raise rcontrolutil.RException('You may only use the "with" syntax with "w.add_frame()", not with manually created frames.')
		
	def __exit__(self, exc_type, exc_value, traceback):
		if exc_type!=None: return False #if an exception actually occurred, let it be raised.
			
		if self._exitWithCallback != None: 
			self._exitWithCallback(self)
			
		self._exitWithCallback = 0 #don't set to None, which means _exitWithCallback was never set
		return False #don't suppress any exceptions.
				
		
	
		